package com.hs.srm.admin.modules.material.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.*;
import com.hs.srm.admin.modules.material.entity.MaterialFactoryEntity;
import com.hs.srm.admin.modules.material.entity.MaterialLeadBackEntity;
import com.hs.srm.admin.modules.material.entity.MaterialLeadEntity;
import com.hs.srm.admin.modules.material.service.MaterialFactoryService;
import com.hs.srm.admin.modules.material.service.MaterialService;
import com.hs.srm.admin.modules.material.service.impl.MaterialLeadBackServiceImpl;
import com.hs.srm.base.common.utils.EntityUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.hs.srm.admin.modules.material.service.MaterialLeadService;
import com.hs.srm.admin.modules.sys.controller.AbstractController;
import com.hs.srm.common.utils.PageUtils;
import com.hs.srm.common.utils.R;

@RestController
@RequestMapping("/materialLead")
public class MaterialLeadController extends AbstractController {

    /**
     * 类序列ID
     */
    private static final long serialVersionUID = -2392337998173025326L;

    @Autowired
    private MaterialLeadService materialLeadService;
    @Autowired
    private MaterialService materialService;
    @Autowired
    private MaterialFactoryService materialFactoryService;
    @Autowired
    private MaterialLeadBackServiceImpl materialLeadBackService;

    /**
     * 查询列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = this.materialLeadService.queryPage(params);
        List<MaterialLeadEntity> list = (List<MaterialLeadEntity>) page.getList();
        String materialDesc=(String)(params.get("materialDesc"));
        if(materialDesc!=null&&materialDesc!=""){
            Iterator it = list.iterator();
            while(it.hasNext()) {
                MaterialLeadEntity materialLeadEntity=(MaterialLeadEntity)it.next();
                if(!materialLeadEntity.getMaterialDesc().contains(materialDesc)){
                    it.remove();
                }
            }
        }
        list = EntityUtils.valToTxt(list);
        page.setList(list);
        return R.ok(page);
    }

    /**
     * 查询列表
     */
    @RequestMapping("/delateMaterial")
    @ResponseBody
    public R delateMaterial(String pkid) {
        boolean flg=this.materialLeadService.delateMaterial(pkid);
       if(flg){
           return R.ok();
       }
       return R.error("删除失败!");
    }
    /**
     *
     * @Description 物料取消方法
     * @Param [materialLeadId]
     * @Return  com.hs.srm.common.utils.R
     * @Author  socik
     * @Date 2019/6/10 11:23
     */
    
    @ResponseBody
    @RequestMapping("/toCancel")
    public R toCancel(@RequestParam(value = "materialLeadId[]") String[] materialLeadId){
      if(materialLeadId!=null){
          List<String> idList = Arrays.asList(materialLeadId);
          for(String pkid:idList){
              MaterialLeadBackEntity materialLeadBackEntity=new MaterialLeadBackEntity();
              MaterialLeadEntity materialLeadEntity=this.materialLeadService.selectById(pkid);
              MaterialFactoryEntity materialFactoryEntity=this.materialFactoryService.selectMaterialFactoryByCode(materialLeadEntity.getMaterialCode(),materialLeadEntity.getWerks());
              try {
                  PropertyUtils.copyProperties(materialLeadBackEntity,materialLeadEntity);
              } catch (Exception e) {
                  e.printStackTrace();
              }
              materialLeadBackEntity.setMaterialLeadId(null);
              this.materialLeadBackService.insert(materialLeadBackEntity);
              if(materialLeadEntity.getLeadnum()>materialLeadEntity.getReturnNum()&&materialLeadEntity.getReturnNum()!=0){
                  return R.error("不可以取消部分发货物料!");
              }else if(materialLeadEntity.getLeadnum()==materialLeadEntity.getReturnNum()){
                    materialFactoryEntity.setIsLead(materialFactoryEntity.getIsLead()-1);
                  materialFactoryEntity.setQunatity(materialFactoryEntity.getQunatity()+materialLeadEntity.getReturnNum());
              }else{
                  materialFactoryEntity.setUsecont(materialFactoryEntity.getUsecont()-materialLeadEntity.getLeadnum());
              }
              this.materialFactoryService.updateById(materialFactoryEntity);
              this.materialLeadService.deleteById(pkid);
          }

      }else {
          return R.error("取消失败!");
      }
        return  R.ok("取消成功!");
    }


}